package ar.com.android.alco.forms;


import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.widget.TextView;
import ar.com.android.alco.dbHelpers.SQLALCOHelper;
import ar.com.android.alco.entities.Alimentos;
import ar.com.android.alco.entities.CategoriaAlimentos;
import ar.com.android.alco.providers.AlimentosProvider;
import ar.com.android.alco.providers.CategoriaAlimentosProvider;

public class CreditosDetalleActivity extends Activity {
	private static final String TAG = "CreditosActivity";
	
	Alimentos alimento;
	
	TextView txtDescripAlimento;
	TextView txtCategoria;
	TextView txtEquivalencia;
	TextView txtValCreditos;
	TextView tituloActivity;
	
    @Override
    public void onStart()
    {
    	super.onStart();
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.creditos_detalle);

        SQLALCOHelper sqlHelper = SQLALCOHelper.getHelper(CreditosDetalleActivity.this);
        sqlHelper.dbOpen();
        
        try {
            Bundle bundle = getIntent().getExtras();

        	//Busco los Objectos del Formlario
            tituloActivity = (TextView) findViewById(R.id.txtTitulo);
        	txtDescripAlimento = (TextView) findViewById(R.id.txtDescripAlimento);
        	txtCategoria = (TextView) findViewById(R.id.txtCategoria);
        	txtEquivalencia = (TextView) findViewById(R.id.txtEquivalencia);
        	txtValCreditos = (TextView) findViewById(R.id.txtValCreditos);
        	
            SpannableString title = new SpannableString(getString(R.string.DetalleCreditos));
            title.setSpan(new UnderlineSpan(), 0, title.length(), 0);
            tituloActivity.setText(title);
        	
            Integer idAlimento =  bundle.getInt("selectedAlimento");
    		
    		AlimentosProvider alimentosDataSource = new AlimentosProvider(CreditosDetalleActivity.this);
    		CategoriaAlimentosProvider categoriaAlimentosProvider = new CategoriaAlimentosProvider(CreditosDetalleActivity.this);
    		
    		alimento = alimentosDataSource.getAlimento(idAlimento);
    		
    		if(alimento != null) {
    			CategoriaAlimentos categoriaAlimentos = categoriaAlimentosProvider.getCategoriaAlimento(alimento.getIdCategoriaAlimento());
    			
    			txtDescripAlimento.setText(alimento.getDescripcion());
    			txtCategoria.setText(categoriaAlimentos.getDescripcion());
    			txtEquivalencia.setText(alimento.getEquivalencia());
    			txtValCreditos.setText(alimento.getCreditos().toString());
    		}

		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
        finally {
        	sqlHelper.dbClose();
        }
    }
}